﻿using UnityEngine;
using System.Collections;

public enum TalkBubbleState
{
	HIDDEN, TALK, THOUGHT
}

[RequireComponent(typeof(Individuo))]
public class TalkBubble : MonoBehaviour
{
	#region Statics

	public static float bubbleTime = 2f;

	#endregion
	#region Variables

	public TalkBubbleState state = TalkBubbleState.HIDDEN;

	public float showTime = 0f;
	public float hideSchedule = 0f;

	private string bubbleText = "This is a Talk Bubble!";

	#endregion
	#region Engine Calls

	void Update()
	{
		//verifica se a bubble deve sumir
		if (state != TalkBubbleState.HIDDEN && Time.time > hideSchedule)
		{
			HideBubble();
			return;
		}

		//update

	}

	bool CheckForHide()
	{
		if (Time.time > hideSchedule)
		{
			HideBubble();
			return true;
		}

		return false;
	}

	#endregion
	#region Methods

	[ContextMenu("Show Talk Bubble")]
	void CallShowTalkBubble()
	{
		ShowTalkBubble("Teste de talk bubble!");
	}

	void ShowTalkBubble(string text)
	{
		Debug.Log("Show Talk Bubble: " + text, this);

		HideBubble();

		//switch TODO colocar o valor certo
		renderer.material.mainTextureOffset = new Vector2(
			renderer.material.mainTextureOffset.x,
			renderer.material.mainTextureOffset.y
			);

		//show
		bubbleText = text;
		state = TalkBubbleState.TALK;
		renderer.enabled = true;

		//delay
		ScheduleHide();
	}

	[ContextMenu("Show Thought Bubble")]
	void CallShowThoughtBubble()
	{
		ShowTalkBubble("Teste de talk bubble!");
	}

	void ShowThoughtBubble(string text)
	{
		Debug.Log("Show Thought Bubble: " + text, this);

		HideBubble();

		//switch TODO colocar o valor certo
		renderer.material.mainTextureOffset = new Vector2(
			renderer.material.mainTextureOffset.x,
			renderer.material.mainTextureOffset.y
			);

		//show
		bubbleText = text;
		state = TalkBubbleState.THOUGHT;
		renderer.enabled = true;

		//delay
		ScheduleHide();
	}

	void ScheduleHide()
	{
		hideSchedule = Time.time + showTime;
	}

	void HideBubble()
	{
		Debug.Log("Hide Bubble", this);

		//hide
		state = TalkBubbleState.HIDDEN;
		renderer.enabled = false;
	}

	#endregion
}
